//
//  HJDSetViewController.m
//  JYHospital
//
//  Created by 胡金东 on 16/4/29.
//  Copyright © 2016年 胡金东. All rights reserved.
//

#import "HJDSetViewController.h"
#import "HJDAboutViewController.h"
//#import "HJDSuggestViewController.h"
#import "HJDShareViewController.h"
#import "HJDLoginViewController.h"
#import "HJDForgetViewController.h"

#import "WXApi.h"
#import "HLActionSheet.h"
static NSString *kLinkURL = @"http://app.wjhfz.com";

static NSString *kLinkTitle = @"专访张小龙：产品之上的世界观";
static NSString *kLinkDescription = @"微信的平台化发展方向是否真的会让这个原本简洁的产品变得臃肿？在国际化发展方向上，微信面临的问题真的是文化差异壁垒吗？腾讯高级副总裁、微信产品负责人张小龙给出了自己的回复。";

@interface HJDSetViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIButton *exitBtn;

@end

@implementation HJDSetViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor getColorWithHexNumber:@"F1F1EE"];
    self.title = @"设置";
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont systemFontOfSize:19],
       NSForegroundColorAttributeName:[UIColor getColorWithHexNumber:@"848484"]}];

    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(popToPre) image:@"︿" HighImage:nil];
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, HJDWidth, 281)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
   //设置tableview 不能滚动
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
     //清空不用的分割线
    [self clearseparatorline:self.tableView];
    
    
    self.exitBtn=[[UIButton alloc]initWithFrame:CGRectMake(0,350 , HJDWidth, 45)];
    [self.exitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.exitBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.exitBtn addTarget:self action:@selector(exitClick) forControlEvents:UIControlEventTouchUpInside];
    self.exitBtn.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.exitBtn];

   
}
//分割线方法，用view视图盖住footview
-(void)clearseparatorline:(UITableView *)tableView
{
    UIView *view=[[UIView alloc]init];
    view.backgroundColor= [UIColor clearColor];
    [self.tableView setTableFooterView:view];
}

-(void)popToPre
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section==0) {
        return 3;
    }else {
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell  *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    
    [ cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            //            cell.textLabel.font=[UIFont systemFontOfSize:FONTSIZE_LEVEL2];
            cell.textLabel.text=@"忘记密码";
           
        }else if (indexPath.row == 1){
//        cell.textLabel.text = @"建议反馈";
            cell.textLabel.text = @"分享";
        
        }else  {
            
            cell.textLabel.text=@"当前版本";
            cell.detailTextLabel.text=@"v 1.0.0";
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }else {

            cell.textLabel.text=@"关于我们";

    }
    
        return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section==0) {
        if (indexPath.row==0) {
            HJDForgetViewController *forget = [[HJDForgetViewController alloc]init];
            [self.navigationController pushViewController:forget animated:YES];
        }else if (indexPath.row == 1){
//            HJDSuggestViewController *suggest=[[HJDSuggestViewController alloc]init];
//            [self.navigationController pushViewController:suggest animated:YES];
//            HJDShareViewController *share = [[HJDShareViewController alloc]init];
//            [self.navigationController pushViewController:share animated:YES];
            NSArray *titles = @[@"微信",@"朋友圈"];
            NSArray *imageNames = @[@"wechat",@"moments"];
            HLActionSheet *sheet = [[HLActionSheet alloc] initWithTitles:titles iconNames:imageNames];
            [sheet showActionSheetWithClickBlock:^(int btnIndex) {
                if (btnIndex == 0) {
                    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]]) {
                        
                        SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
                        sendReq.bText = NO;//不使用文本信息
                        sendReq.scene = 0;
                        //创建分享内容对象
                        WXMediaMessage *urlMessage = [WXMediaMessage message];
                        urlMessage.title = kLinkTitle;//分享标题
                        urlMessage.description = kLinkDescription;//分享描述
                        [urlMessage setThumbImage:[UIImage imageNamed:@"关于我们"]];//分享图片,使用SDK的setThumbImage方法可压缩图片大小
                        
                        //创建多媒体对象
                        WXWebpageObject *webObj = [WXWebpageObject object];
                        webObj.webpageUrl = kLinkURL;//分享链接
                        
                        
                        //完成发送对象实例
                        urlMessage.mediaObject = webObj;
                        sendReq.message = urlMessage;
                        
                        //发送分享信息
                        [WXApi sendReq:sendReq];
                    }else{
                        [MBProgressHUD showError:@"你还没有安装微信,请安装微信"];
                    }
                    
                }else if (btnIndex == 1){
                    SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
                    sendReq.bText = NO;//不使用文本信息
                    sendReq.scene = 1;
                    //创建分享内容对象
                    WXMediaMessage *urlMessage = [WXMediaMessage message];
                    urlMessage.title = kLinkTitle;//分享标题
                    urlMessage.description = kLinkDescription;//分享描述
                    [urlMessage setThumbImage:[UIImage imageNamed:@"testImg"]];//分享图片,使用SDK的setThumbImage方法可压缩图片大小
                    
                    //创建多媒体对象
                    WXWebpageObject *webObj = [WXWebpageObject object];
                    webObj.webpageUrl = kLinkURL;//分享链接
                    
                    //完成发送对象实例
                    urlMessage.mediaObject = webObj;
                    sendReq.message = urlMessage;
                    
                    //发送分享信息
                    [WXApi sendReq:sendReq];
                    
                }else{
                    [MBProgressHUD showError:@"你还没有安装微信,请安装微信"];
                }
            } cancelBlock:^{
                NSLog(@"取消");
            }];

        }
    }else{
        HJDAboutViewController *about=[[HJDAboutViewController alloc]init];
        [self.navigationController pushViewController:about animated:YES];

    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

        return HJDVerticalMargin;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

-(void)exitClick
{
    UIAlertController * alertControl = [UIAlertController alertControllerWithTitle:@"确定退出？" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        HJDLoginViewController *loginVC = [[HJDLoginViewController alloc]init];
        [self presentViewController:loginVC animated:YES completion:nil];
        NSUserDefaults *defa = [NSUserDefaults standardUserDefaults];
        [defa removeObjectForKey:@"uId"];
        
        
        
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertControl addAction:action];
    [alertControl addAction:cancelAction];
    [self presentViewController:alertControl animated:YES completion:nil];
    
}




@end
